﻿using System;
using System.Collections.Generic;
using System.Linq;

using ET;
using Marx;

namespace Demo
{
	public class LoginTokenComponent : Entity, IAwake
	{
		public class KeyInfo
		{
			public string Account;
			public long AccountId;
			public int LogicAppId;
			public HashSet<long> Players = new HashSet<long>();
		}

		private readonly Dictionary<long, KeyInfo> sessionKey = new Dictionary<long, KeyInfo>();

		public void Add(long key, string account, long accountId, int logicId, IEnumerable<long> players)
		{
			var info = new KeyInfo() { Account = account, AccountId = accountId, LogicAppId = logicId };
			foreach (var player in players)
			{
				info.Players.Add(player);
			}

			this.sessionKey.Add(key, info);
			this.TimeoutRemoveKey(key).Coroutine();
		}

		public KeyInfo Get(long key)
		{
			KeyInfo account;
			this.sessionKey.TryGetValue(key, out account);
			return account;
		}

		public void Remove(long key)
		{
			this.sessionKey.Remove(key);
		}

		private async ETTask TimeoutRemoveKey(long key)
		{
			await Game.Scene.GetComponent<TimerComponent>().WaitAsync(20000);
			this.sessionKey.Remove(key);
		}
	}
}
